<template>
    <div class="class-add-or-update">
        <el-dialog :title="!dataForm.classifyId ? '新增' : '修改'" :close-on-click-modal="false"  @close="close" :visible.sync="visible">
            <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                label-width="80px">
                <el-form-item label="分类名称" prop="classificationName">
                    <el-input v-model="dataForm.classifyName" placeholder="分类名称"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close()">取消</el-button>
                <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                classifyId: 0,
                classifyName: '',
                merchantId: 0
            },
            dataRule: {
                classifyName: [
                    { required: true, message: '分类不能为空', trigger: 'blur' }
                ]
            },
        }
    },
    methods: {
        init(id) {
            this.visible = true
            this.dataForm.classifyId = id || 0
            if (this.dataForm.classifyId !== 0) {
                this.$http({
                    url: this.$http.adornUrl(`/web/classfication/get/${this.dataForm.classifyId}`),
                    method: 'get',
                }).then(({ data }) => {
                    this.dataForm.classifyName = data.classfication.classifyName,
                    this.dataForm.merchantId = data.classfication.merchantId,
                    this.dataForm.classifyId=data.classfication.classifyId
                })
            }

        },
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/web/classfication/${this.dataForm.classifyId ===0 ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData({
                                'classifyId': this.dataForm.classifyId ,
                                'merchantId': this.dataForm.merchantId || undefined,
                                'classifyName': this.dataForm.classifyName
                            })
                        }).then(({ data }) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                }
            })
        },
        close() {
            this.dataForm={
                classifyId: 0,
                classifyName: '',
                merchantId: 0
            },
            this.visible=false
        },
    }
}
</script>

<style></style>